var maxSubArray = function (nums) {
  let pre = 0,
    maxAns = nums[0];
  nums.forEach((x) => {
    pre = Math.max(pre + x, x);
    console.log("pre", pre);
    maxAns = Math.max(maxAns, pre);
    console.log("maxAns", maxAns);
  });
  return maxAns;
};

// 测试用例
const arr = [1, -2, 3, 10, -4, 7, 2, -5];
console.log(maxSubArray(arr)); // 输出: 18
